package com.ydgk.controller;

import com.ydgk.bean.*;
import com.ydgk.service.*;
import com.ydgk.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class DispatcherControllerForPortal {
    @Autowired
    private KindsService kindsService;

    @Autowired
    private KindsTypeService kindsTypeService;

    @Autowired
    private FurnitureService furnitureService;

    @Autowired
    private ViewService viewService;

    @Autowired
    private DetailService detailService;

    @Autowired
    private FurnitureKindsService furnitureKindsService;

    @Autowired
    private OrderService orderService;

    @Autowired
    private UserService userService;

    public static List<Pre_order> accountPre = new ArrayList<Pre_order>();

    @ModelAttribute
    public void getFurnitureDetail(@RequestParam(value = "fid" , required = false , defaultValue = "0") Integer fid, Map map){
        if(fid != 0){
            Furniture furniture = furnitureService.findById(fid);
            List<View> viewList = viewService.findByFid(fid);
            List<Detail> detailList = detailService.findByFid(fid);
            List<FurnitureKinds> furnitureKindsList = furnitureKindsService.findByFid(fid);
            map.put("furniture",furniture);
            map.put("viewList",viewList);
            map.put("detailList",detailList);
            map.put("furnitureKindsList",furnitureKindsList);
        }else{
            map.put("furniture",null);
            map.put("viewList",null);
            map.put("detailList",null);
            map.put("furnitureKindsList",null);
        }
    }

    @ModelAttribute
    public void getPreList(HttpSession session, Map map){
        User user = (User)session.getAttribute("user");
        if(user != null){
            int uid = user.getUid();
            List<Pre_order> pre_orderList = orderService.getPre(uid);
            map.put("pre_orderList",pre_orderList);
        }else{
            map.put("pre_orderList",null);
        }
    }

    @ModelAttribute
    public void getPre_order(@RequestParam(value = "poid" , required = false , defaultValue = "0") Integer poid){
      if(poid != 0){
          Pre_order pre_order = orderService.findPreById(poid);
          accountPre.clear();
          accountPre.add(pre_order);
      }
    }

    @ModelAttribute
    public void getOrderInfo(@RequestParam(value = "oid" , required = false , defaultValue = "0") Integer oid,Map map){
        if(oid != 0){
            Order order = orderService.findOrderById(oid);
            OrderInfo orderInfo = orderService.getOrderInfo(order);
            map.put("orderInfo",orderInfo);
        }
    }

    @ModelAttribute
    public void getAddress(HttpSession session, Map map){
        User user = (User)session.getAttribute("user");
        if(user != null){
            int uid = user.getUid();
            List<Address> addressList = userService.getAdd(uid);
            map.put("addressList",addressList);
        }else{
            map.put("addressList",null);
        }
    }

    @ModelAttribute
    public void getAttribute(@RequestParam(value = "kid" , required = false , defaultValue = "0") Integer kid,
            @RequestParam(value = "ktid" , required = false , defaultValue = "0") Integer ktid,Map map){
        if(kid != 0){
            Kinds kinds = kindsService.findById(kid);
            map.put("kinds",kinds);
        }else{
            map.put("kinds",null);
        }

        if(ktid != 0){
            KindsType kindsType = kindsTypeService.findById(ktid);
            Kinds kinds = kindsService.findById(kindsType.getK_kid());
            map.put("kindsType",kindsType);
            map.put("kinds",kinds);
        }else{
            map.put("kindsType",null);
        }
    }

    @RequestMapping("/toKindsGoods")
    public String toKindsTypeGoods(Kinds kinds){
        if(kinds != null){
            return "Portal/kindsGoods";
        }else{
            return "redirect:/toPortalLogin";
        }
    }

    @RequestMapping("/toKindsGoods2")
    public String toKindsTypeGoods2(Kinds kinds){
        if(kinds != null){
            return "Portal/kindsGoods2";
        }else{
            return "redirect:/toPortalLogin";
        }
    }

    @RequestMapping("/toKindsTypeGoods")
    public String toKindsTypeGoods(Kinds kinds , KindsType kindsType){
        if(kindsType != null && kinds != null){
            return "Portal/kindsTypeGoods";
        }else{
            return "redirect:/toPortalLogin";
        }
    }


    @RequestMapping("/toFurnitureDetail")
    public String toFurnitureDetail(Kinds kinds , KindsType kindsType , Furniture furniture, List<View> viewList, List<Detail> detailList, List<FurnitureKinds> furnitureKindsList){
        if(kindsType != null && kinds != null && furniture != null && viewList != null&& detailList != null&& furnitureKindsList != null){
            return "Portal/common/furnitureDetail";
        }else{
            return "redirect:/toPortalLogin";
        }
    }

    @RequestMapping("/toTrolley")
    public String toTrolley(List<Pre_order> pre_orderList, Map map){
        if(pre_orderList != null){
            List<TrolleyInfo> trolleyInfoList = new ArrayList<>();
            int count = 0;
            int stock = 0;
            for (Pre_order pre:pre_orderList) {
                count = pre.getCount();
                FurnitureKinds furnitureKinds = furnitureKindsService.findById(pre.getFk_fkid());
                stock = furnitureKinds.getStock();
                if(count > stock){
                    orderService.deletePre_order(pre.getPoid());
                }else{
                    Furniture furniture = furnitureService.findById(furnitureKinds.getF_fid());
                    TrolleyInfo trolleyInfo = new TrolleyInfo(pre,furniture,furnitureKinds);
                    trolleyInfoList.add(trolleyInfo);
                }
            }
            map.put("trolleyInfoList",trolleyInfoList);
            return "Portal/trolley";
        }else{
            return "redirect:/toPortalLogin";
        }
    }

    @RequestMapping("/toAddress")
    public String toAddress(List<Address> addressList,Map map) {
        List<AddressInfo> addressInfoList = new ArrayList<>();
        if (addressList != null) {
            for (Address add:addressList) {
                AddressInfo addressInfo = userService.getAddressInfo(add);
                addressInfoList.add(addressInfo);
            }
            map.put("addressInfoList",addressInfoList);
            return "Portal/address";
        } else {
            return "redirect:/toPortalLogin";
        }
    }

    @ResponseBody
    @RequestMapping(value = "/getAccountPre")
    public AjaxResult getAccountPre(@RequestParam(value = "poids[]")  Integer[]  poids) {
        AjaxResult ajaxResult = new AjaxResult();
        accountPre.clear();
        for (int i = 0; i < poids.length; i++) {
            accountPre.add(orderService.findPreById(poids[i]));
        }
        ajaxResult.setSuccess(true);
        return ajaxResult;
    }


    @RequestMapping(value = "/toAccount")
    public String getAccountInfo(List<Address> addressList, Map map){
        if(addressList.size()>0){
            List<TrolleyInfo> trolleyInfoList = new ArrayList<>();
            for (Pre_order pre:accountPre) {
                FurnitureKinds furnitureKinds = furnitureKindsService.findById(pre.getFk_fkid());
                Furniture furniture = furnitureService.findById(furnitureKinds.getF_fid());
                TrolleyInfo trolleyInfo = new TrolleyInfo(pre,furniture,furnitureKinds);
                trolleyInfoList.add(trolleyInfo);
            }
            List<AddressInfo> addressInfoList = new ArrayList<>();
            for (Address add:addressList) {
                AddressInfo addressInfo = userService.getAddressInfo(add);
                addressInfoList.add(addressInfo);
            }
            AccountInfo accountInfo = new AccountInfo(trolleyInfoList,addressInfoList);
            map.put("accountInfo",accountInfo);
            return "Portal/account";
        }else{
            return "Portal/address";
        }
    }

    @RequestMapping("/toOrderDetail")
    public String toOrderDetail(OrderInfo orderInfo){
        if(orderInfo != null){
            return "Portal/orderDetail";
        }else{
            return "redirect:/toPortalLogin";
        }
    }

    @RequestMapping("/toLogistics")
    public String toLogistics(OrderInfo orderInfo){
        if(orderInfo != null){
            return "Portal/logistics";
        }else{
            return "redirect:/toPortalLogin";
        }
    }

}
